﻿using AngleSharp.Dom;
using FieldTools.Client.Command;
using FieldTools.Client.Model.Page;
using FieldTools.Util;
using log4net;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using System;
using System.Collections.Generic;
using System.Windows;

namespace FieldTools.Client.ViewModel.Page
{
    /// <summary>
    /// 
    /// </summary>
    public class SettingViewModel : ISingletonDependency
    {
        private readonly ILogger<SettingViewModel> _logger = App.serviceProvider.GetRequiredService<ILogger<SettingViewModel>>();
        /// <summary>
        /// 初始构造
        /// </summary>
        public SettingViewModel()
        {
            //MainColors = new() { "#FFFFCC", "#CCFFFF", "#FFCCCC", "#CCCCCC", "#999933", "#0099CC", "#99CCFF" };
            //_logger = logger;
            //装载基础配置
            obj = CacheHelper.GetOrSetCache("SettingModel", new SettingModel());
            Console.WriteLine("1231414");
        }

        /// <summary>
        /// 数据回调委托
        /// </summary>
        public MessageHelp.delegateMessage CallbackMethod;


        #region 实体

        public SettingModel obj { get; set; } = new();

        #endregion

        #region 事件
        private BaseCommand click;
        /// <summary>
        /// 事件
        /// </summary>
        public BaseCommand Click
        {
            get
            {
                click ??= new BaseCommand(new Action<object>(o =>
                    {
                        var str = "获取到配置\r\n" + obj.ToJson();
                        _logger.LogInformation(str);
                        Console.WriteLine(str+"（控制台打印）");
                        obj.IsAutoStartTcpService = true;
                    }));
                return click;
            }
        }

        private BaseCommand click2;
        /// <summary>
        /// 事件2
        /// </summary>
        public BaseCommand Click2
        {
            get
            {
                click2 ??= new BaseCommand(new Action<object>(o =>
                {
                    obj.IsAutoStartTcpService = !obj.IsAutoStartTcpService;
                    obj.IsAutoStartUdpService = !obj.IsAutoStartUdpService;
                }));
                return click2;
            }
        }

        #endregion

        #region 方法
        public void SaveConfig()
        {
            try
            {
                if (obj != null)
                {
                    CacheHelper.SetCache("SettingModel", obj);
                    //MessageBox.Show("保存成功");
                }
                else
                {
                    //MessageBox.Show("配置文件异常");
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show("保存失败" + ex);
            }
        }
        #endregion
    }
}
